package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.example.springboot.controller.DTO.LoginDTO;
import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.IUserService;
import com.example.springboot.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserService implements IUserService {

    @Autowired
    UserMapper userMapper;


    private static final String DEFAULT_PASS = "00000000";

    private static final String PASS_SALT = "oac";



    @Override
    public List<User> list() {
        return userMapper.list();
    }

    @Override
    public PageInfo<User> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<User> users = userMapper.listByCondition(baseRequest);
        return new PageInfo<>(users);
    }

    @Override
    public void save(User user) {
        Date date = new Date();
        if (user.getCardID() == null) {
            user.setCardID(DateUtil.format(date, "yyyyMMdd") + Math.abs(IdUtil.fastSimpleUUID().hashCode()));
        }
        user.setUpdateTime(date);
        user.setPassword(securePassword(user.getPassword()));
        userMapper.save(user);
    }

    @Override
    public void changePassword(PasswordRequest request) {
        request.setNewPass(securePassword(request.getNewPass()));
        int i = userMapper.updatePassword(request);
        if (i <= 0) {
            throw new ServiceException("密码修改失败");
        }
    }

    //登录
    @Override
    public LoginDTO login(LoginRequest request) {
        User user = null;
        try {
            user = userMapper.getByUsername(request.getUsername());
        } catch (Exception e) {
            log.error("根据用户名{} 查询出错", request.getUsername());
            throw new ServiceException("用户名错误");
        }
        //验证密码是否为空
        if (user == null) {
            throw new ServiceException("用户名或密码错误");
        }

        //验证密码是否正确
        String securePassword = securePassword(request.getPassword());
        if (!securePassword.equals(user.getPassword())) {
            throw new ServiceException("用户名或密码错误");
        }

        if(user.getVipID().equals(false)){
            throw new ServiceException("账户已被冻结，请联系管理员处理");
        }

        LoginDTO loginDTO = new LoginDTO();
        BeanUtils.copyProperties(user, loginDTO);

        String token = TokenUtils.genToken(String.valueOf(user.getId()), user.getPassword());

        loginDTO.setToken(token);
        return loginDTO;
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }


    //注册
    @Override
    public void register(User user) {
        Date date = new Date();
        if (user.getCardID() == null) {
            user.setCardID(DateUtil.format(date, "yyyyMMdd") + Math.abs(IdUtil.fastSimpleUUID().hashCode()));
        }
        user.setPassword(securePassword(user.getPassword()));
        user.setUpdateTime(date);
        userMapper.register(user);
    }


    //存储、解密密码 设置md5加密 加盐
    private String securePassword(String password) {
        return SecureUtil.md5(password + PASS_SALT);
    }
}
